const { get,post } = require("../../utils/request");
Page({
  data: {
    surveyTitle: "用户体验调查问卷",
    questions: [
      {
        problemNumber: 'q1',
        index: 1,
        type: 'radio',
        problemTitle: '您对试乘试驾车辆的交接流程是否满意？',
        options: [
          { label: '满意', value: '满意' },
          { label: '一般', value: '一般' },
          { label: '不满意', value: '不满意' }
        ],
        problemAnswer: ''
      },
      {
        problemNumber: 'q2',
        index: 2,
        type: 'radio',
        problemTitle: '工作人员对车辆功能、配置的介绍是否清晰、详细？',
        options: [
          { label: '详细', value: '详细' },
          { label: '一般', value: '一般' },
          { label: '不清晰', value: '不清晰' }
        ],
        problemAnswer: ''
      },
      {
        problemNumber: 'q3',
        index: 3,
        type: 'radio',
        problemTitle: '工作人员对试乘试驾路线及注意事项的讲解是否到位?',
        options: [
          { label: '到位', value: '到位' },
          { label: '一般', value: '一般' },
          { label: '不到位', value: '不到位' }
        ],
        problemAnswer: ''
      },
      {
        problemNumber: 'q4',
        index: 4,
        type: 'radio',
        problemTitle: '试乘试驾车辆的整洁程度和车况是否令您满意?',
        options: [
          { label: '满意', value: '满意' },
          { label: '一般', value: '一般' },
          { label: '不满意', value: '不满意' }
        ],
        problemAnswer: ''
      },
      {
        problemNumber: 'q5',
        index: 5,
        type: 'radio',
        problemTitle: '您对车辆的外观、内饰设计是否满意?',
        options: [
          { label: '满意', value: '满意' },
          { label: '一般', value: '一般' },
          { label: '不满意', value: '不满意' }
        ],
        problemAnswer: ''
      },
      {
        problemNumber: 'q6',
        index: 6,
        type: 'radio',
        problemTitle: '您对车辆的动力表现（如起步、加速、超车）是否满意?',
        options: [
          { label: '满意', value: '满意' },
          { label: '一般', value: '一般' },
          { label: '不满意', value: '不满意' }
        ],
        problemAnswer: ''
      },{
        problemNumber: 'q7',
        index: 7,
        type: 'radio',
        problemTitle: '车辆的操控性（如转向灵活性、刹车灵敏度、方向盘手感等）如何?',
        options: [
          { label: '非常好', value: '非常好' },
          { label: '一般', value: '一般' },
          { label: '不好', value: '不好' }
        ],
        problemAnswer: ''
      },
      {
        problemNumber: 'q8',
        index: 8,
        type: 'radio',
        problemTitle: '车辆的舒适性（如座椅舒适度、噪音控制、空调效果等）是否令您满意?',
        options: [
          { label: '满意', value: '满意' },
          { label: '一般', value: '一般' },
          { label: '不满意', value: '不满意' }
        ],
        problemAnswer: ''
      },
     /*  {
        problemNumber: 'q9',
        index: 9,
        type: 'radio',
        problemTitle: '本次试驾的产品是否达到您的预期?',
        options: [
          { label: '满意', value: '满意' },
          { label: '一般', value: '一般' },
          { label: '不满意', value: '不满意' }
        ],
        problemAnswer: ''
      }, */
      {
        problemNumber: 'q9',
        index: 9,
        type: 'text',
        problemTitle: '您对本次试乘试驾体验还有哪些其他的意见或建议?',
        problemSuggestion: ''
      }
    ],
    currentIndex: 0,
    answers: {},
    progressPercent: 0
  },

  // 单选题选择
  radioChange: function(e) {
    this.data.questions.forEach((item)=> {
      if(item.problemNumber === e.currentTarget.dataset.item.problemNumber) {
        if('problemAnswer' in item) {
          item.problemAnswer = e.detail.value
          item.wxId = wx.getStorageSync('openid')
        }
      }  
    })
  },

  // 填空题输入
  textInput: function(e) {
    debugger
    this.data.questions.forEach((item)=> {
      if(item.problemNumber === e.currentTarget.dataset.questionId) {
        if('problemSuggestion' in item) {
          item.problemSuggestion = e.detail.value
          item.wxId = wx.getStorageSync('openid')
        }
      }  
    })
  },

  
  // 下一题或提交
  nextQuestion: function() {
    this.submitSurvey();
  },

  // 提交问卷
  submitSurvey: function() {
    let flag = false
    for(let index = 0; index < this.data.questions.length; index++){
      const item = this.data.questions[index]
      if(item.type === 'radio' && item.problemAnswer === '') {
        wx.showToast({
          title: '第 ' + (index + 1) + ' 道题没有选项！',
          icon: 'none'
        });
        flag = true
        break
      }
    }
    if(flag) {
      return
    }
    wx.showLoading({
      title: '提交中...',
    });
    console.log(this.data.questions)
    
    post('/system/questionnaire/add', this.data.questions).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        wx.showToast({
          title: '问卷调查提交成功...',
          icon: 'none'
        });
        wx.navigateTo({
          url: '/pages/index/index'
        });
      }
    }).catch(err => {
      wx.hideLoading();
    });
  }
});